extends Control

const HeartbeatLine := preload("res://Scenes/Menu/heartbeat_line.gd")

@export var title:AudioStream


@export var main_scene:String
@export var transition_packed:PackedScene
@export var heartbeat_length: float = 900.0
@export var heartbeat_amplitude: float = 60.0
@export var heartbeat_frequency: float = 0.6
@export var heartbeat_spike_count: int = 1

@onready var start_button: Button = %StartButton
@onready var continue_button: Button = %ContinueButton
@onready var setting_button: Button = %SettingButton
@onready var quit_button: Button = %QuitButton
@onready var thanks_button: Button = %ThanksButton
@onready var setting_conpoment: Setting = $SettingConpoment
@onready var mode_button: Button = %ModeButton
@onready var bg_texture: TextureRect = $"../BgTexture"
@onready var challenge_button: Button = %ChallengeButton

var heartbeat_line: Line2D




func _ready() -> void:
	var tween = create_tween()
	tween.tween_property(bg_texture,"modulate:a",1.0,.5).from(0).set_ease(Tween.EASE_OUT)
	MusicPlayer.play_bgm(title)
	if DialogHandler.star >= 4 || Global.current_setting.get("UNLOCK",false):
		challenge_button.disabled = false
		Global.current_setting.set("UNLOCK", true)
		SaveManager.save_set()
	_add_heartbeat_line()
	challenge_button.pressed.connect(on_challenge_pressed)
	mode_button.pressed.connect(on_mode_pressed)
	setting_button.pressed.connect(on_setting_pressed)
	start_button.pressed.connect(on_start_pressed)
	continue_button.pressed.connect(on_continue_pressed)
	quit_button.pressed.connect(on_quit_pressed)
	thanks_button.pressed.connect(on_thanks_pressed)
	if ResourceLoader.exists("user://savegame.tres") == false:
		continue_button.disabled = true
		continue_button.modulate.a = .2


func on_start_pressed() -> void:
	var transition :Transition= transition_packed.instantiate()
	transition.is_show_prograss = true
	transition.next_scene_path = main_scene
	Global.current_stage_name = ""
	add_child(transition)
	

func on_setting_pressed() -> void:
	if setting_conpoment.visible:
		setting_conpoment.is_in_game = false
		setting_conpoment.exit()
	else:
		setting_conpoment.is_in_game = false
		setting_conpoment.enter()

func on_continue_pressed() -> void:
	var transition :Transition= transition_packed.instantiate()
	transition.is_show_prograss = true
	transition.next_scene_path = main_scene
	add_child(transition)
	SaveManager.load_game()


func on_mode_pressed() -> void:
	GuiTransitions.go_to("Difficult")

func on_quit_pressed() -> void:
	get_tree().quit()
	
func on_thanks_pressed() -> void:
	GuiTransitions.go_to("Thanks")

func on_challenge_pressed() -> void:
	Global.current_stage_name = "stage5"
	var transition :Transition= transition_packed.instantiate()
	transition.is_show_prograss = true
	transition.next_scene_path = main_scene
	add_child(transition)


func _add_heartbeat_line() -> void:
	if heartbeat_line:
		return
	heartbeat_line = HeartbeatLine.new()
	heartbeat_line.line_length = heartbeat_length
	heartbeat_line.amplitude = heartbeat_amplitude
	heartbeat_line.frequency = heartbeat_frequency
	heartbeat_line.spike_count = heartbeat_spike_count
	heartbeat_line.z_index = 1
	add_child(heartbeat_line)
	_update_heartbeat_position()
	_connect_heartbeat_resized()

func _update_heartbeat_position() -> void:
	if heartbeat_line and is_instance_valid(bg_texture):
		var target_size = bg_texture.size
		heartbeat_line.position = Vector2((target_size.x - heartbeat_length) * 0.45, target_size.y * 0.8)


func _connect_heartbeat_resized() -> void:
	var callable = Callable(self, "_update_heartbeat_position")
	if not resized.is_connected(callable):
		resized.connect(callable)
	if not bg_texture.resized.is_connected(callable):
		bg_texture.resized.connect(callable)
